/*
 * @Description: 数字签名
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-09-17 17:03:29
 * @LastEditors: ziwei
 * @LastEditTime: 2021-09-17 17:52:42
 */
let {generateKeyPairSync, createVerify,createSign} = require('crypto');
let passphrase = 'passphrase'
let rsa = generateKeyPairSync('rsa',{
  modulusLength:1024,
  publicKeyEncoding:{
    type:'spki',
    format:'pem',//base64格式的私钥
  },
  privateKeyEncoding:{
    type:'pkcs8',//公钥
    format:'pem',
    cipher:'aes-256-cbc',
    passphrase:'passphrase'
  }
});
let file = 'file';
//先创建签名对象
let signObj = createSign('RSA-SHA256');
//放入文件内容
signObj.update(file);
let sign = signObj.sign({key:rsa.privateKey,format:'pem',passphrase:'passphrase'},'hex')
console.log("signsign",sign);

//创建验证签名对象
let verifyObj = createVerify('RSA-SHA256');
//放入文件内容
verifyObj.update(file);
//验证签名是否合法
let isValid = verifyObj.verify(rsa.publicKey,sign,'hex');
console.log(isValid);
//内部是这样实现，我是如何知识签名是否正确呢？
//验证方先拿到文件file,然后用pubcliKey计算签名，如果根对方的sign匹配，则验证通过
